package de.szut.traineeslife.fragments;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
import de.szut.traineeslife.R;
import de.szut.traineeslife.utils.DateHelper;
import de.szut.traineeslife.utils.DialogListener;
import de.szut.traineeslife.utils.ProfilHelper;

public class WelcomeDialogFragment extends DialogFragment {

	private final String TAG = "welcomedialog";
	private Dialog mDialog;
	private Calendar calendar = Calendar.getInstance();
	private EditText nameEdit;
	private EditText educationEdit;
	private EditText educationArtEdit;
	private RadioGroup artOfReport;
	private Button nextBtn;
	private String educationArt;
	private String education;
	private String name;
	private DatePicker fromPicker;
	private String fromString;
	private Date fromDate;
	private DatePicker toPicker;
	private String toString;
	private Date toDate;
	private boolean isWeeklyRep = false;
	private boolean isDailyRep = false;
	private ProfilHelper mProfilHelper;
	private DateHelper mDateHelper;
	private DialogListener dialogListener;

	public WelcomeDialogFragment() {
	}

	/**Is The First Dialog to Create the Usersprofil
	 * @param dialog
	 * @param profilHelper
	 * @param dateHelper
	 */
	public WelcomeDialogFragment(Dialog dialog, ProfilHelper profilHelper, DateHelper dateHelper) {
		mDialog = dialog;
		mProfilHelper = profilHelper;
		mDateHelper = dateHelper;
	}

	public static WelcomeDialogFragment newInstance(Dialog dialog, ProfilHelper profilHelper, DateHelper dateHelper) {
		WelcomeDialogFragment dialogFragment = new WelcomeDialogFragment(dialog, profilHelper, dateHelper);
		return dialogFragment;

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.dialog_welcome, container, false);
		mDialog = getDialog();
		mDialog.setTitle(R.string.welcome_dialog);
		mDialog.setCancelable(false);
		mDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
		nameEdit = (EditText) v.findViewById(R.id.welcome_name);
		educationEdit = (EditText) v.findViewById(R.id.welcome_education);
		educationArtEdit = (EditText) v.findViewById(R.id.welcome_education_art);
		fromPicker = (DatePicker) v.findViewById(R.id.welcome_PickerFrom);
		fromPicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new OnDateChangedListener() {

			@Override
			public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
				calendar.clear();
				calendar.set(year, monthOfYear, dayOfMonth);
				setFromDate(calendar.getTime());
				SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yy");
				setFromString(formatter.format(getFromDate()));


			}
		});
		toPicker = (DatePicker) v.findViewById(R.id.welcome_PickerTo);
		toPicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new OnDateChangedListener() {

			@Override
			public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
				calendar.clear();
				calendar.set(year, monthOfYear, dayOfMonth);
				setToDate(calendar.getTime());
				SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yy");
				setToString(formatter.format(getToDate()));

			}
		});
		
		artOfReport = (RadioGroup) v.findViewById(R.id.welcome_report_art);
		artOfReport.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.welcome_daily:
					setDailyRep(true);
					setWeeklyRep(false);
					break;
				case R.id.welcome_weekly:
					setWeeklyRep(true);
					setDailyRep(false);
					break;

				default:
					break;
				}
				
			}
		});

		nextBtn = (Button) v.findViewById(R.id.welcome_next);
		nextBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if ((nameEdit.getText().toString().length() > 1) && (educationEdit.getText().toString().length() > 1) && (educationArtEdit.getText() != null) && (getToString() != null)
						&& (getFromString() != null) &&((isDailyRep == true && isWeeklyRep == false)||(isWeeklyRep == true && isDailyRep == false))) {
					setName(nameEdit.getText().toString());
					setEducation(educationEdit.getText().toString());
					setEducationArt(educationArtEdit.getText().toString());
					fillProfilHelper();
					fillDateHelper();
					dialogListener.startSearchModelDialog();
				} else {
					Toast.makeText(getActivity(), "Bitte füllen sie alle Felder", Toast.LENGTH_SHORT).show();
				}
			}
		});
		return v;
	}
	
	/**
	 * Fills the Profilhelper with the data that the user has entered
	 */
	private void fillProfilHelper(){
		mProfilHelper.setUserName(getName());
		mProfilHelper.setUserEducationSecotr(getEducation());
		mProfilHelper.setUserArtEducation(getEducationArt());
		mProfilHelper.setEducationFromDate(getFromString());
		mProfilHelper.setEducationToDate(getToString());
		mProfilHelper.setWeeklyReport(isWeeklyRep);
		mProfilHelper.setDailyReport(isDailyRep);
	}
	
	/**
	 * Makes the Datahelper ready for TraineesLifeActivity
	 */
	private void fillDateHelper(){
		mDateHelper.setEducationFrom(getFromDate());
		mDateHelper.setEducationTo(getToDate());
	}
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (!(activity instanceof DialogListener)) {
			throw new RuntimeException("Activity must implement WelcomeDialogListener");
		}
		dialogListener = (DialogListener) getActivity();
	}

	public String getTAG() {
		return TAG;
	}

	public Date getFromDate() {
		return fromDate;
	}

	public void setFromDate(Date fromDate) {
		this.fromDate = fromDate;
	}

	public Date getToDate() {
		return toDate;
	}

	public void setToDate(Date toDate) {
		this.toDate = toDate;
	}

	public String getFromString() {
		return fromString;
	}

	public void setFromString(String fromString) {
		this.fromString = fromString;
	}

	public String getToString() {
		return toString;
	}

	public void setToString(String toString) {
		this.toString = toString;
	}

	public String getEducation() {
		return education;
	}

	public void setEducation(String education) {
		this.education = education;
	}

	public String getEducationArt() {
		return educationArt;
	}

	public void setEducationArt(String educationArt) {
		this.educationArt = educationArt;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isWeeklyRep() {
		return isWeeklyRep;
	}

	public void setWeeklyRep(boolean isWeeklyRep) {
		this.isWeeklyRep = isWeeklyRep;
	}

	public boolean isDailyRep() {
		return isDailyRep;
	}

	public void setDailyRep(boolean isDailyRep) {
		this.isDailyRep = isDailyRep;
	}


}
